/*
 * Copyright (c) 2020-2022. the original authors and DEPSEA.ORG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.depsea.log.notification;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.depsea.log.model.RequestInfo;

/**
 * @author Wang Chengwei
 * @since 1.0.0 [2022/12/2 14:28]
 */
@NoArgsConstructor
@Data
public class Message {

    /**
     * 请求地址
     */
    private String requestUri;

    /**
     * 追踪链路Id
     */
    private String traceId;

    /**
     * 应用名称
     */
    private String applicationName;

    /**
     * 接口名称 -- swagger 注解中获取
     */
    private String apiName;

    /**
     * 请求参数，本来是map但是为了能够在存储到ES中时，不因数据类型和字段发生变更而导致一些错误，所以使用字符串
     */
    private String requestParameterMap;

    /**
     * Spring将请求参数解析后注入到方法参数后的数据
     */
    private String methodParameterMap;

    /**
     * 错误消息
     */
    private String errorMessage;

    /**
     * 异常类
     */
    private String exceptionName;


    public Message(RequestInfo requestInfo) {
        this.requestUri = requestInfo.getRequestUri();
        this.traceId = requestInfo.getTraceId();
        this.applicationName = requestInfo.getApplicationName();
        this.apiName = requestInfo.getApiName();
        this.requestParameterMap = requestInfo.getRequestParameterMap();
        this.methodParameterMap = requestInfo.getMethodParameterMap();
        this.errorMessage = requestInfo.getErrorMessage();
        this.exceptionName = requestInfo.getExceptionName();
    }

    public String toPlainMessage() {
        return "服务：" + this.applicationName + "\n" +
                "TraceId：" + this.traceId + "\n" +
                "接口名称：" + this.apiName + "\n" +
                "请求地址：" + this.requestUri + "\n" +
                "异常类：" + exceptionName + "\n" +
                "错误信息：" + this.errorMessage;
    }

    public String toMarkdownMessage() {
        return "**服务：** " + this.applicationName + "\n\n" +
                "**TraceId：** " + this.traceId + "\n\n" +
                "**接口名称：** " + this.apiName + "\n\n" +
                "**请求地址：** " + this.requestUri + "\n\n" +
                "**异常类：** " + exceptionName + "\n\n" +
                "**错误信息：** " + this.errorMessage + "\n";
    }

    public String getTitle() {
        return "接口[" + this.apiName + "]请求异常";
    }

}
